package com.fly.datasyn.service.impl;

import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fly.core.entity.Page;
import com.fly.core.entity.PageUtils;
import com.fly.datasyn.dao.HealthDAO;
import com.fly.datasyn.entity.Health;
import com.fly.datasyn.service.HealthService;

@Service
@Transactional
public class HealthServiceImpl implements HealthService
{
    @Autowired
    private HealthDAO healthDAO;
    
    @Override
    public Health get(Long id)
    {
        return healthDAO.getOne(id);
    }
    
    @Override
    public void saveOrUpdate(Health health)
    {
        health.setTime(DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        healthDAO.save(health);
    }
    
    @Override
    public void delete(Long id)
    {
        healthDAO.deleteById(id);
    }
    
    @Override
    public List<Health> findAll(Page page)
    {
        org.springframework.data.domain.Page<Health> springDataPage = healthDAO.findAll(PageUtils.createPageable(page));
        page.setTotalCount(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }
    
    @Override
    public List<Health> findByExample(Specification<Health> specification, Page page)
    {
        org.springframework.data.domain.Page<Health> springDataPage = healthDAO.findAll(specification, PageUtils.createPageable(page));
        page.setTotalCount(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }
}
